package com.gzmu.face.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gzmu.face.entity.Device;
import com.gzmu.face.entity.Person;
import com.gzmu.face.entity.PersonPicInfo;
import com.gzmu.face.mapper.PersonMapper;
import com.gzmu.face.service.DeviceService;
import com.gzmu.face.service.PersonService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gzmu.face.tools.HttpResult;
import com.gzmu.face.tools.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobo Yan
 * @since 2020-04-19
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private PersonMapper personMapper;

    @Transactional
    @Override
    public Map insertPersonUp(Person person) {
        JSONArray roomStr = (JSONArray) JSON.parse(person.getRoomNum());
        Map<String, Object> map = new HashMap<>();
        map.put("code",201);
        for (int i = 0; i<roomStr.size();i++ ) {
            JSONObject room = (JSONObject) roomStr.get( i );
            int id = room.getIntValue( "value" );
            Device device = deviceService.selectById( id );
            if (device != null) {
                person.setDeviceID( device.getDeviceID() );
                person.setRemark( 0 );
                personMapper.insertPerson( person );
                person.setPersonUUID( person.getId().toString());
                personMapper.updateAllColumnById( person );
                PersonPicInfo faceAddInfo = new PersonPicInfo();
                Map<String, String> headParams = new HashMap<>();
                headParams.put( "name", "admin" );
                headParams.put( "passwd", "admin" );
                faceAddInfo.setOperator( "AddPerson" );
                faceAddInfo.setPicinfo( person.getPicinfo() );
                person.setPicURI( null );
                person.setPicinfo( null );
                ObjectMapper mapper = new ObjectMapper();
                faceAddInfo.setInfo( person );
                try {
                    System.out.println( "======================faceAddInfo:" + mapper.writeValueAsString( faceAddInfo ) );
                    HttpResult result = HttpUtil.doPost( "http://" + device.getIPAddr() + "/action/AddPerson",
                            headParams, mapper.writeValueAsString( faceAddInfo ) );
                    if (result.getStatusCode() == 200) {
                        JSONObject jsonObject = JSONObject.parseObject( result.getBody() );
                        map.put( "code", jsonObject.get( "code" ) );
                        map.put( "info", jsonObject.get( "info" ) );
                    }
                } catch (Exception e) {
                    map.put( "code", 202 );
                    e.printStackTrace();
                }
            }
        }
        return map;
    }

    @Transactional
    @Override
    public Map deletePersonUp(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code",201);
        Person person = personMapper.selectById( id );
        Device device = deviceService.selectById(person.getRoomNum());
        Map<String,String> headParams = new HashMap<>();
        Map<String,Object> info = new HashMap<>();
        headParams.put( "name","admin" );
        headParams.put( "passwd", "admin" );
        PersonPicInfo faceAddInfo = new PersonPicInfo();
        faceAddInfo.setOperator( "DeletePerson" );
        info.put( "DeviceID" ,person.getDeviceID());
        info.put( "TotalNum" ,"1" );
        info.put( "IdType" ,person.getIdType());
        info.put( "LiPersonUUIDbID" ,person.getId());
        faceAddInfo.setInfo(info);
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println("======================faceAddInfo:" + mapper.writeValueAsString(faceAddInfo));
            HttpResult result = HttpUtil.doPost( "http://"+ device.getIPAddr() +"/action/DeletePerson",
                    headParams, mapper.writeValueAsString(faceAddInfo));
            if(result.getStatusCode() == 200){
                JSONObject jsonObject = JSONObject.parseObject(result.getBody());
                map.put("code", jsonObject.get( "code"));
                map.put("info",jsonObject.get( "info"));
            }
        } catch (Exception e) {
            map.put("code",202);
            e.printStackTrace();
        }
        personMapper.deleteById( id );
        return map;
    }
}
